package com.ljcalc.business.components.fetchers;

import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XNIException;

import com.ljcalc.business.components.SPTTCombination;

public class SPTTHistoryResultItemListHandler extends
		HistoryResultItemListHandler {

	boolean isInScope = false;
	private boolean lastIsSpan = false;
	
	public SPTTHistoryResultItemListHandler() {
		super();
		lastIsSpan = false;
	}
	
	public void startElement(QName element, XMLAttributes attributes,
			Augmentations augs) throws XNIException {
		String clazz = attributes.getValue("class");
		if(!isInScope && clazz!=null && clazz.equalsIgnoreCase("match_num")){
			isInScope = true;
		}
		
		if(isInScope&& clazz!=null&&clazz.equalsIgnoreCase("black_txt_16b")){
			startRecording = true;
		}
		
		if(startRecording){
			lastIsSpan = element.rawname.equalsIgnoreCase("SPAN");
		}
		
	
	}
	
	public void endElement(QName element, Augmentations augs)
			throws XNIException {
		if(startRecording && lastIsSpan && element.rawname.equalsIgnoreCase("SPAN")){
    		startRecording = false;
    		isInScope = false;
		}
	}
	
	@Override
	public void characters(XMLString text, Augmentations augs)
			throws XNIException {
		// TODO Auto-generated method stub
		if(startRecording){
			
    		if(comb==null||comb.isFull()){
    			comb = new SPTTCombination();
    		}
    		
    		list = extract(list, comb, text);
    		
    		while(list.remove(new SPTTCombination()));

		}
	}
}
